#include "cameramanager.h"

#include "systemconfiguration.h"
#include "component_log.h"
#include "visual_data.h"

#include "visualsystem.h"

CameraManager::CameraManager(QObject* parent)
	: ComponentBase(parent)
{
	m_name = u8"相机管理组件";
	m_type = Component::component_camera;
}

CameraManager::~CameraManager()
{}


// 组件初始化处理函数
int CameraManager::ComponentInit()
{
	auto setting = syscfg->GetVisualSetting();
	if (setting.cameras.size() <= 0) {
		log_error("Not find Camera setting, so  dont need creat camera component");
		return COMMON_ERROR;
	}
	if (setting.picPath.isEmpty() || setting.visPath.isEmpty()) {
		SetErrmsg("图片存储路径或者视觉服务路径配置为空，无法创建相机管理组件", false);
		return SERIOUS_ERROR;
	}

	for (auto& cam : setting.cameras) {
		VisualInfo info = {};
		info.id = cam.id;
		info.ip = cam.ip;
		info.brand = cam.type;
		info.algName = cam.alg;
		info.biaoding = cam.biaoding;
		info.path = setting.picPath;
		info.visPath = setting.visPath;
		auto temp = new VisualSystem(this);
		if (temp->Init(info)) {
			log_trace("camera [{}] init successed", cam.id);
			m_cams.insert(cam.id, temp);
		} else {
			SetErrmsg(QString("相机%1初始化失败，请检查对应配置是否正确").arg(cam.id), false);
			return SERIOUS_ERROR;
		}
	}


	return 0;
}

void* CameraManager::GetPoint(const QString& id)
{
	if (m_cams.contains(id)) {
		return m_cams.value(id);
	}

	return nullptr;
}

/*自检接口*/
bool CameraManager::ComponentCheckSelfState()
{
	return true;
}
/*复位接口*/
bool CameraManager::ComponentSelfReset()
{

	return true;
}


